package com.syni.mdd.yhd.common.all.service.impl;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsAgentDao;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsBusinessVerRecordDao;
import com.syni.mdd.yhd.common.all.entity.BmsAgent;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessVerRecord;
import com.syni.mdd.yhd.common.all.entity.BmsDxGroupBuy;
import com.syni.mdd.yhd.common.all.entity.BmsDxGroupBuyUse;
import com.syni.mdd.yhd.common.all.entity.coupon.BmsUserReceCoupon;
import com.syni.mdd.yhd.common.all.service.BmsAgentService;
import com.syni.mdd.yhd.common.all.service.BmsBusinessVerRecordService;
import com.syni.mdd.yhd.common.all.service.BmsDxGroupBuyService;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @className
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsBusinessVerRecordServiceImpl implements BmsBusinessVerRecordService {
    @Autowired
    BmsBusinessVerRecordDao bmsBusinessVerRecordDao;

    @Autowired
    BmsDxGroupBuyService bmsDxGroupBuyService;

    @Override
    public BmsBusinessVerRecordDao getBaseDao() {
        return bmsBusinessVerRecordDao;
    }

    @Override
    public synchronized void addBuyVerRecord(List<BmsDxGroupBuyUse> bmsDxGroupBuyUses,Integer businessId) {
        List<BmsBusinessVerRecord> bv = new LinkedList<>();
        if(bmsDxGroupBuyUses != null && bmsDxGroupBuyUses.size() > 0){
            BmsDxGroupBuyUse bmsDxGroupBuyUse = bmsDxGroupBuyUses.get(0);
            Set<String> codes = bmsDxGroupBuyUses.stream().map(BmsDxGroupBuyUse::getGroupBuyCode).collect(Collectors.toSet());
            BmsDxGroupBuy bmsDxGroupBuy = bmsDxGroupBuyService.getGroupBuyById(bmsDxGroupBuyUse.getGroupBuyId());
            if(bmsDxGroupBuy != null) {
                Map<String, Object> conditionMap = new HashMap<>();
                conditionMap.put("orderId", bmsDxGroupBuyUse.getOrderId());
                conditionMap.put("verWay", bmsDxGroupBuyUse.getUseWay());
                conditionMap.put("verTime", bmsDxGroupBuyUse.getUseTime());
                List<BmsBusinessVerRecord> bmsBusinessVerRecords = bmsBusinessVerRecordDao.findAll(new SpecificationUtils(conditionMap));
                if (!(bmsBusinessVerRecords != null && bmsBusinessVerRecords.size() > 0)) {
                    BmsBusinessVerRecord bmsBusinessVerRecord = new BmsBusinessVerRecord();
                    bmsBusinessVerRecord.setType(Constants.VER_TYPE_MUY);
                    bmsBusinessVerRecord.setOrderId(bmsDxGroupBuyUse.getOrderId());
                    bmsBusinessVerRecord.setBusinessId(businessId);
                    bmsBusinessVerRecord.setCouponName(bmsDxGroupBuy.getGroupName());
                    bmsBusinessVerRecord.setVerTime(bmsDxGroupBuyUse.getUseTime());
                    bmsBusinessVerRecord.setVerWay(bmsDxGroupBuyUse.getUseWay());
                    bmsBusinessVerRecord.setVerNum(bmsDxGroupBuyUses.size());
                    bmsBusinessVerRecord.setVerName(bmsDxGroupBuyUse.getNickName());
                    bmsBusinessVerRecord.setVerCodes(JSON.toJSONString(codes));
                    bmsBusinessVerRecord.setExpireTime(bmsDxGroupBuy.getExpireTime());
                    bv.add(bmsBusinessVerRecord);
                }
            }
        }
        if(bv.size() > 0){
            bmsBusinessVerRecordDao.saveAll(bv);
        }
    }

    @Override
    public void addReceVerRecord(BmsUserReceCoupon bmsUserReceCoupon) {
        if(bmsUserReceCoupon != null){
            Map<String, Object> conditionMap = new HashMap<>();
            conditionMap.put("receId", bmsUserReceCoupon.getId());
            conditionMap.put("verWay", bmsUserReceCoupon.getVerWay());
            conditionMap.put("verTime", bmsUserReceCoupon.getUseTime());
            List<BmsBusinessVerRecord> bmsBusinessVerRecords = bmsBusinessVerRecordDao.findAll(new SpecificationUtils(conditionMap));
            if (!(bmsBusinessVerRecords != null && bmsBusinessVerRecords.size() > 0)) {
                BmsBusinessVerRecord bmsBusinessVerRecord = new BmsBusinessVerRecord();
                bmsBusinessVerRecord.setType(Constants.VER_TYPE_RECE);
                bmsBusinessVerRecord.setReceId(bmsUserReceCoupon.getId());
                bmsBusinessVerRecord.setBusinessId(bmsUserReceCoupon.getBusinessId());
                bmsBusinessVerRecord.setCouponName(bmsUserReceCoupon.getBmsBusinessCoupon().getCouponName());
                bmsBusinessVerRecord.setVerTime(bmsUserReceCoupon.getUseTime());
                bmsBusinessVerRecord.setVerWay(bmsUserReceCoupon.getVerWay());
                bmsBusinessVerRecord.setVerName(bmsUserReceCoupon.getVerName());
                bmsBusinessVerRecord.setVerNum(1);
                bmsBusinessVerRecord.setExpireTime(bmsUserReceCoupon.getExpireTime());
                Set<String> codes = new HashSet<>();
                codes.add(bmsUserReceCoupon.getConsumerCode());
                bmsBusinessVerRecord.setVerCodes(JSON.toJSONString(codes));
                bmsBusinessVerRecordDao.save(bmsBusinessVerRecord);
            }
        }
    }
}
